home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5051 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  64 lines

  1. Path: newsfeed.ACO.net!alijku06!news
  2. From: Roland Exler <R.Exler@jk.uni-linz.ac.at>
  3. Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
  4. Subject: Re: float != float and floats as return types
  5. Date: 1 Feb 1996 07:40:13 GMT
  6. Organization: Institute for el. Measurement, University of Linz, Austria
  7. Message-ID: <4epqot$fj1@alijku06.edvz.uni-linz.ac.at>
  8. References: <4ej9lb$mpc@fu-berlin.de>
  9. NNTP-Posting-Host: sensor4.emt.uni-linz.ac.at
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15. axl@zedat.fu-berlin.de (Axel Thimm) wrote:
  16. >The next C++ example gives me surprising results:
  17. >    #include <iostream.h>
  18. >    #include <iomanip.h>
  19. >    #include <math.h>
  20. >    float quad( float );
  21. >    int main() {
  22. >      for( int i=0; i<10; ++i ) {
  23. >        float a, b, c;
  24. >        a = i/13.123123;
  25. >        b = a*a;
  26. >        c = quad(a);
  27. >        cout << (b - c) << '\t';
  28. >        cout << (b - a*a) << '\t';
  29. >        cout << (c - quad(a)) << '\n';
  30. >      }
  31. >      return 0;
  32. >    }
  33. >    float quad( float x ) { return x*x; }
  34.  
  35. >At first I thought all numbers should be zeros. The middle column might
  36. >not be zero, if the compiler calculates in higher precision than what
  37. >the variables are, so an intermediate storage (b) might loose some
  38. >digits, but I cannot understand what happens with c! Both times a
  39. >function is called, that _is_not_ inlined, so in both cases the same
  40. >loss of digits should be observed.
  41. >Any ideas?
  42.  
  43. Hi Axel,
  44.  
  45. Maybe the result of quad is returned on the coprocessor-stack, so it's uses 
  46. with the full precicion even if the result is declared as float. (The 
  47. coprocessor does _all_ computations using double or extended dependend on an 
  48. internal flag.) In this case all floating-point results are truncated only if 
  49. _stored_ to a var with less precicion. If you want to see more compile your 
  50. program with gcc -c t_prec -save-temps and take a look on the file t_prec.s 
  51. (the assembler-output for your program).
  52.  
  53. Roland
  54.  
  55. +---------------------------------------+---------------------------+
  56. I Roland Exler                          I EMAIL:                    I
  57. I Universitaet Linz                     I R.Exler@jk.uni-linz.ac.at I
  58. I Institut fuer Elektrische Messtechnik I                           I
  59. I Altenbergerstr. 69                    I Phone:                    I
  60. I A-4040 Linz, AUSTRIA                  I + 43 732 2468 9205        I
  61. +---------------------------------------+---------------------------+
  62.  
  63.  
  64.